{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed=1234567890\n",
    "random.seed(seed)\n",
    "np.random.seed(seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_file_path = 'DATA_FILE_PATH' in os.environ\n",
    "data_file_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using Hard-Coded Configuration for data_file_path\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'C:/Development/kaggle--home-credit-default-risk/data/'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Set configuration based on environment variables\n",
    "# os.environ['DATA_FILE_PATH']\n",
    "data_file_path = 'DATA_FILE_PATH' in os.environ\n",
    "if data_file_path:\n",
    "    print('Using Environment Variable for data_file_path')\n",
    "else:\n",
    "    data_file_path = 'C:/Development/kaggle--home-credit-default-risk/data/'\n",
    "    print('Using Hard-Coded Configuration for data_file_path')\n",
    "data_file_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'application_train.csv'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_file_name = 'application_train.csv'\n",
    "data_file_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_table(data_file_path + data_file_name, sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_current='df'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_record_id_column_name = 'SK_ID_CURR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'AuthorName': 'JamesBalcomb',\n",
       " 'DataFileName': 'application_train.csv',\n",
       " 'DataFrameName': 'application_train',\n",
       " 'DateTime': '2018-06-15 12:03:00',\n",
       " 'RecordId': '144669',\n",
       " 'RecordIdColumnName': 'SK_ID_CURR',\n",
       " 'ValueChangedColumnName': 'CODE_GENDER',\n",
       " 'ValueChangedFrom': 'XNA',\n",
       " 'ValueChangedTo': 'F'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_data_changes = {\n",
    "    'DataFileName':'application_train.csv',\n",
    "    'DataFrameName':'application_train',\n",
    "    'RecordIdColumnName':'SK_ID_CURR',\n",
    "    'RecordId':'144669',\n",
    "    'ValueChangedColumnName':'CODE_GENDER',\n",
    "    'ValueChangedFrom':'XNA',\n",
    "    'ValueChangedTo':'F',\n",
    "    'AuthorName':'JamesBalcomb',\n",
    "    'DateTime':'2018-06-15 12:03:00'\n",
    "}\n",
    "dict_data_changes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'AuthorName': 'JamesBalcomb',\n",
       "  'DataFileName': 'application_train.csv',\n",
       "  'DataFrameName': 'application_train',\n",
       "  'DateTime': '2018-06-15 12:03:00',\n",
       "  'RecordId': '144669',\n",
       "  'RecordIdColumnName': 'SK_ID_CURR',\n",
       "  'ValueChangedColumnName': 'CODE_GENDER',\n",
       "  'ValueChangedFrom': 'XNA',\n",
       "  'ValueChangedTo': 'F'}]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_dict_data_changes = [{\n",
    "    'DataFileName':'application_train.csv',\n",
    "    'DataFrameName':'application_train',\n",
    "    'RecordIdColumnName':'SK_ID_CURR',\n",
    "    'RecordId':'144669',\n",
    "    'ValueChangedColumnName':'CODE_GENDER',\n",
    "    'ValueChangedFrom':'XNA',\n",
    "    'ValueChangedTo':'F',\n",
    "    'AuthorName':'JamesBalcomb',\n",
    "    'DateTime':'2018-06-15 12:03:00'\n",
    "}]\n",
    "list_dict_data_changes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1529179987.0716887\n",
      "Sat Jun 16 15:13:07 2018\n",
      "1529179987.073688\n",
      "2018-06-16T15:13:07.073688\n",
      "1529179987\n",
      "2018-06-16 15:13:07.079689\n"
     ]
    }
   ],
   "source": [
    "# Using module time ()\n",
    "import time\n",
    "ts = time.time()  # number of seconds since the epoch\n",
    "print(ts)\n",
    "#print(time.strftime(\"%Y-%m-%d %H:%M:%S\", ts))  # TypeError: Tuple or struct_time argument required\n",
    "ts_human_readable = time.ctime(ts)\n",
    "print(ts_human_readable)\n",
    "# Using module datetime\n",
    "import datetime;\n",
    "ts = datetime.datetime.now().timestamp()  # number of seconds since the epoch\n",
    "print(ts)\n",
    "#print(time.strftime(\"%Y-%m-%d %H:%M:%S\", ts))  # TypeError: Tuple or struct_time argument required\n",
    "ts_human_readable = datetime.datetime.fromtimestamp(ts).isoformat()\n",
    "print(ts_human_readable)\n",
    "# Using module calendar\n",
    "import calendar;\n",
    "import time;\n",
    "ts = calendar.timegm(time.gmtime())\n",
    "print(ts)\n",
    "#print(time.strftime(\"%Y-%m-%d %H:%M:%S\", ts))  # TypeError: Tuple or struct_time argument required\n",
    "# using Pandas module\n",
    "import pandas\n",
    "ts = pandas.Timestamp.now(tz=None)  # current time local to tz\n",
    "print(ts)\n",
    "#print(time.strftime(\"%Y-%m-%d %H:%M:%S\", ts))  # TypeError: Tuple or struct_time argument required"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# Use-Case: Manually Change One Value\n",
    "# Use-Case: Change All Values In A Column Based On The Existing Value (e.g., np.nan, if \"XAN\")\n",
    "def changes_value(DataFileName=None,\n",
    "                  DataFrameName=None,\n",
    "                  RecordIdColumnName=None,\n",
    "                  RecordId=None,\n",
    "                  ValueChangedColumnName=None,\n",
    "                  ValueChangedFrom=None,\n",
    "                  ValueChangedTo=None,\n",
    "                  AuthorName=None):\n",
    "    \"\"\"\n",
    "    params:\n",
    "        DataFileName:\n",
    "        DataFrameName:\n",
    "        RecordIdColumnName:\n",
    "        RecordId:\n",
    "        ValueChangedColumnName:\n",
    "        ValueChangedFrom:\n",
    "        ValueChangedTo:\n",
    "        AuthorName:\n",
    "        -DateTime\n",
    "    \"\"\"\n",
    "    if not DataFileName:\n",
    "        DataFileName = data_file_name\n",
    "    if not DataFrameName:\n",
    "        DataFileName = df_current\n",
    "    if not RecordIdColumnName:\n",
    "        RecordIdColumnName = df_record_id_column_name\n",
    "\n",
    "\n",
    "def change_values_matching(DataFileName=None,\n",
    "                           DataFrameName=None,\n",
    "                           RecordIdColumnName=None,\n",
    "                           TargetColumnName=None,\n",
    "                           ValueToChangeFrom=None,\n",
    "                           ValueToChangeTo=None,\n",
    "                           AuthorName=None):\n",
    "    \"\"\"\n",
    "    params:\n",
    "        DataFileName:\n",
    "        DataFrameName:\n",
    "        RecordIdColumnName:\n",
    "        -RecordId:\n",
    "        ValueChangedColumnName:\n",
    "        ValueChangedFrom:\n",
    "        ValueChangedTo:\n",
    "        AuthorName:\n",
    "        -DateTime\n",
    "    \"\"\"\n",
    "    if not DataFileName:\n",
    "        DataFileName = data_file_name\n",
    "    if not DataFrameName:\n",
    "        DataFileName = df_current\n",
    "    if not RecordIdColumnName:\n",
    "        RecordIdColumnName = df_record_id_column_name\n",
    "    \n",
    "    date_time = pandas.Timestamp.now(tz=None)\n",
    "    \n",
    "    print('DataFileName: ', DataFileName)\n",
    "    print('DataFrameName: ', DataFrameName)\n",
    "    print('RecordIdColumnName: ', RecordIdColumnName)\n",
    "    print('TargetColumnName: ', TargetColumnName)\n",
    "    print('ValueToChangeFrom: ', ValueToChangeFrom)\n",
    "    print('ValueToChangeTo: ', ValueToChangeTo)\n",
    "    print('AuthorName: ', AuthorName)\n",
    "    print('DateTime: ', date_time)\n",
    "    \n",
    "    dict_data_changes = {\n",
    "    'DataFileName':DataFileName,\n",
    "    'DataFrameName':DataFrameName,\n",
    "    'RecordIdColumnName':RecordIdColumnName,\n",
    "    'RecordId':None,\n",
    "    'ValueChangedColumnName':TargetColumnName,\n",
    "    'ValueChangedFrom':ValueToChangeFrom,\n",
    "    'ValueChangedTo':ValueToChangeTo,\n",
    "    'AuthorName':AuthorName,\n",
    "    'DateTime':ts\n",
    "    }\n",
    "    \n",
    "    #if not list_dict_data_changes:\n",
    "    #    list_dict_data_changes = dict_data_changes\n",
    "    #else:\n",
    "    #    list_dict_data_changes.append(dict_data_changes)\n",
    "    \n",
    "    print('list_dict_data_changes: ', list_dict_data_changes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "changes_value(\n",
    "    DataFileName='application_train.csv',\n",
    "    DataFrameName='application_train',\n",
    "    RecordIdColumnName='SK_ID_CURR',\n",
    "    RecordId='144669',\n",
    "    ValueChangedColumnName='CODE_GENDER',\n",
    "    ValueChangedFrom='XNA',\n",
    "    ValueChangedTo='F',\n",
    "    AuthorName='JamesBalcomb'\n",
    "    #DateTime='2018-06-15 12:03:00'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFileName:  application_train.csv\n",
      "DataFrameName:  df\n",
      "RecordIdColumnName:  SK_ID_CURR\n",
      "TargetColumnName:  FLAG_OWN_CAR\n",
      "ValueToChangeFrom:  Y\n",
      "ValueToChangeTo:  1\n",
      "AuthorName:  James Balcomb\n",
      "DateTime:  2018-06-16 15:17:30.965782\n",
      "list_dict_data_changes:  [{'DataFileName': 'application_train.csv', 'DataFrameName': 'application_train', 'RecordIdColumnName': 'SK_ID_CURR', 'RecordId': '144669', 'ValueChangedColumnName': 'CODE_GENDER', 'ValueChangedFrom': 'XNA', 'ValueChangedTo': 'F', 'AuthorName': 'JamesBalcomb', 'DateTime': '2018-06-15 12:03:00'}, {'DataFileName': 'application_train.csv', 'DataFrameName': 'df', 'RecordIdColumnName': 'SK_ID_CURR', 'RecordId': None, 'ValueChangedColumnName': 'FLAG_OWN_CAR', 'ValueChangedFrom': 'Y', 'ValueChangedTo': 1, 'AuthorName': 'James Balcomb', 'DateTime': Timestamp('2018-06-16 15:13:07.079689')}, {'DataFileName': 'application_train.csv', 'DataFrameName': 'df', 'RecordIdColumnName': 'SK_ID_CURR', 'RecordId': None, 'ValueChangedColumnName': 'FLAG_OWN_CAR', 'ValueChangedFrom': 'Y', 'ValueChangedTo': 1, 'AuthorName': 'James Balcomb', 'DateTime': Timestamp('2018-06-16 15:13:07.079689')}]\n"
     ]
    }
   ],
   "source": [
    "#pd.Series(np.where(df.FLAG_OWN_CAR.values == 'Y', 1, 0), df.index)\n",
    "change_values_matching(\n",
    "    DataFileName=data_file_name,\n",
    "    DataFrameName=df_current,\n",
    "    TargetColumnName='FLAG_OWN_CAR',\n",
    "    ValueToChangeFrom='Y',\n",
    "    ValueToChangeTo=1,\n",
    "    AuthorName='James Balcomb'\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
